package net.wanho.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.UserDTO;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-12-18 14:11
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping
    public AjaxResult findAll(){
        List<User> list = userService.list();
        return AjaxResult.success(list);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable int id){
        userService.removeById(id);
        return AjaxResult.success();
    }

    @PostMapping
    public AjaxResult save(@RequestBody User user){
        userService.save(user);
        System.out.println(user); // 返回主键
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult modify(@RequestBody User user){
        userService.updateById(user);
        return AjaxResult.success();
    }

    @GetMapping("/login")
    public AjaxResult login(String username, String password){
        User user = userService.login(username, password);
        return AjaxResult.success(user);
    }

    @GetMapping("/query")
    public AjaxResult query(UserDTO userDTO){
        List<User> list = userService.findByCondition(userDTO);
        return AjaxResult.success(list);
    }

    @GetMapping("/findByPage")
    public AjaxResult findByPage(@RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "5") int pageSize,
                                 String username){
       Page<User> page = userService.findByPage(pageNum, pageSize, username);
       return AjaxResult.success(page);
    }
}

